var bmob = function(option){
    option = Object.assign({
        model:false,
        recv:false,        // 1
        initData:false,    // 1
        component:false,   // 2
        makeQuery:false,
        limit:10,
    },option);
    if(!option.model)return false;
    var ending = false;
    var Model = Bmob.Object.extend(option.model);
    var query = new Bmob.Query(Model);
    var component = option.component
    if(component){
        if(!component.state)component.state={data:[]}
        else if(!component.state.data)component.state.data = []
    }
    return {
        loadmore(refresh){
            return new Promise((resolve,reject)=>{
                if(refresh){
                    ending = false;
                    if(component.state)component.state.data = []
                    if(option.initData)option.initData()
                }
                if(ending){
                    reject();
                    return false;
                }
                ending = true;
                if(option.makeQuery)option.makeQuery(query)
                if(component.state)query.skip(component.state.data.length)
                query.limit(option.limit)
                query.find({
                    success:(res)=>{
                        if(res.length>0){
                            ending = false;
                        }
                        if(component.state){
                            component.state.data = component.state.data.concat(res)
                            component.setState({})
                        }
                        if(option.recv)option.recv(res);
                        resolve(res);
                    },
                    error:(res)=>{
                        ending = false;
                        reject(res);
                    }
                })
            })
        },
    }
}
module.exports = bmob
